package cn.yuxirey.spotsview;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * 圆点数据
 *
 * @author Reyux
 * @date 2023/1/7 下午 3:26
 */
public class SpotData<T extends Spot> {
    protected final List<T> spotList = new ArrayList<>();
    private DataChangeObserver mDataChangedObserver;

    public void addSpot(T spot) {
        this.spotList.add(spot);
    }

    public void removeSpot() {
        spotList.remove(spotList.size() - 1);
    }

    public void clear() {
        this.spotList.clear();
    }

    public void setData(List<T> spotData) {
        this.spotList.clear();
        if (spotData != null) {
            this.spotList.addAll(spotData);
        }
    }

    @Nullable
    public T getItem(int i) {
        if (i >= 0 && i < spotList.size()) {
            return spotList.get(i);
        }
        return null;
    }

    public List<T> getDataList() {
        return spotList;
    }

    public int getItemCount() {
        return spotList.size();
    }

    public void notifyDataChanged() {
        if (mDataChangedObserver != null) {
            mDataChangedObserver.onDataChanged();
        }
    }

    void setDataChangedObserver(DataChangeObserver dataChangedObserver) {
        this.mDataChangedObserver = dataChangedObserver;
    }
}
